<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-DRC-trends-widget" :style="getStyle('head')">
      <!--科室信息-->
      <div class="dept-room-default" :class="getClassName">
        <p class="dept" v-if="deptRoomshow('deptName')" :style="getStyle('deptName')">
          <ScrollX>{{ getRoomDeptInfo.deptName || '\u00a0' }}</ScrollX>
        </p>
        <p class="room" v-if="deptRoomshow('roomName')" :style="getStyle('roomName')">
          <ScrollX>{{ getRoomDeptInfo[optionsInfo['roomNameKey']] || getRoomDeptInfo.roomName || '\u00a0' }}</ScrollX>
        </p>
      </div>
      <!--医生信息-->
      <div class="doctor-list-wrap" :key="widgetKey">
        <component
            :field="field"
            :devType="devType"
            :is="componentName"
            :doctorData="getRoomDoctorInfo"
        />
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {pxToVw} from '@/utils/common';
import emitter from "@/utils/designer/emitter";
import {buildParentApi} from "@/utils/designer/util";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import ScrollY from "@c/public/seamlessScroll/ScrollY.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

import trends2DoctorList1 from './trends2Comps/trends2DoctorList-1.vue';
import trends2DoctorList2 from './trends2Comps/trends2DoctorList-2.vue';
import trends2DoctorList3 from './trends2Comps/trends2DoctorList-3.vue';
import trends2DoctorList4 from './trends2Comps/trends2DoctorList-4.vue';
import trends2DoctorList5 from './trends2Comps/trends2DoctorList-5.vue';
import trends2DoctorList6 from './trends2Comps/trends2DoctorList-6.vue';
import {v4 as uuid} from "uuid";

let buildParent = buildParentApi();
export default {
  name: "scene-DRC-trends2-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollY,
    ScrollX,
    CompOperate,
    trends2DoctorList1,
    trends2DoctorList2,
    trends2DoctorList3,
    trends2DoctorList4,
    trends2DoctorList5,
    trends2DoctorList6,
  },

  props: {
    widget: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      number: 1,
      widgetKey: uuid(),
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg"),
      pause: require("@a/images/design/pause.png"),
    }
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            return Obj;
          case 'picture':
            Reflect.set(Obj, 'height', pxToVw(info.imageHeight));
            Reflect.set(Obj, 'width', pxToVw(info.imageWidth));
            Reflect.set(Obj, 'borderRadius', pxToVw(info.imageRadius));
            return Obj;
          case 'deptName':
            Reflect.set(Obj, 'fontSize', pxToVw(info.deptNameFontSize));
            return Obj;
          case 'roomName':
            Reflect.set(Obj, 'fontSize', pxToVw(info.roomNameFontSize));
            return Obj;
        }
      }
    },

    // 获取科室&诊室信息
    getRoomDeptInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDeptInfo'];
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
    },

    //顶部科室和诊室的claasName
    getClassName() {
      let {defaultShowCol = ['deptName', 'roomName']} = this.optionsInfo;
      let length = defaultShowCol?.length;
      switch (length) {
        case 1:
          return 'dept-room-1'
        case 2:
          return 'dept-room-default'
      }
    },

    //顶部科室诊室显示控制
    deptRoomshow() {
      return function (row) {
        let {defaultShowCol = ['deptName', 'roomName']} = this.optionsInfo;
        return defaultShowCol.includes(row)
      }
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let devType = (this.devType);
      let {doctorNumber = 1} = this.optionsInfo;
      let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
      let listInfo = devType? roomDoctorInfo.apiData : roomDoctorInfo.mockData;
      let orderInfo = this.$lodash.orderBy(listInfo, ['sortNumber']);
      if (!!(orderInfo?.length)) {
        if (devType) {
          if (orderInfo?.length >= 7) {
            return orderInfo.slice(0, 6);
          } else {
            return orderInfo.slice(0);
          }
        } else {
          return orderInfo.slice(0, doctorNumber);
        }
      } else {
        return Array(1).fill({});
      }
    },

    // 加载对应的动态组件
    componentName() {
      let doctorInfo = (this.getRoomDoctorInfo);
      if (doctorInfo?.length) {
        return `trends2DoctorList${doctorInfo?.length}`
      } else {
        return `trends2DoctorList1`
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDeptInfo":
          console.log(`科室信息-${data}`)
          this.states = true;
          this.$store.dispatch('setRoomDeptInfo', this.$lodash.cloneDeep(data))
          break
        case "roomDoctorInfo":
          console.log(`医生信息-${data}`);
          this.widgetKey = uuid();
          this.$store.dispatch('setRoomDoctorInfo', this.$lodash.cloneDeep(data))
          break
      }
    },
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = (val.options.config)
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
//科室&诊室通用样式
.scene-DRC-trends-widget {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 28px 30px;
  box-sizing: border-box;
  color: #333;
  height: 1670px;
  font-size: 40px;
  background: transparent;
  overflow: hidden;

  //科室&诊室
  .dept-room-default {
    padding: 10px 0 10px 0;
    background: #0059B3;
    width: 100%;
    overflow: hidden;
    flex-shrink: 0;
    box-sizing: border-box;
    height: 300px;
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    align-self: center;

    p {
      color: #FFFFFF;
      font-size: 80px;
      font-weight: 500;
      text-align: center;
      @include singleEllipsis(100%);
    }

    .dept {
      font-size: 82px;
      line-height: 1.35;
      @include singleEllipsis(100%);
    }

    .room {
      font-size: 112px;
      line-height: 1.35;
      @include singleEllipsis(100%);
    }
  }

  //科室||诊室
  .dept-room-1 {
    display: flex;
    align-items: center;
    align-self: center;

    .dept,
    .room {
      font-size: 134px;
      line-height: 1.35;
    }
  }

  .doctor-list-wrap {
    background: transparent;
    flex-shrink: 0;
    height: calc(100% - 300px - 30px);
    margin-top: 30px;
    box-sizing: border-box;
    overflow: hidden;
  }
}
</style>
